package abq2.jic.jic.plugin.operate.course;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.*;

public class AIChooseCourse implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String s, Map<String, String> map) {
        JSONArray jsonArray = new JSONArray();
        Map result = null;
        if (s.equals("AIChooseCourse")) {
            String numChooseStr = map.get("number");
            int numChoose = Integer.parseInt(numChooseStr);

            for (int i = 0; i < numChoose; i++) {
                JSONObject jsonObject = new JSONObject();

                DynamicObject myCourse = BusinessDataServiceHelper.newDynamicObject("abq2_my_course");
                StringBuilder sb1 = new StringBuilder();
                sb1.append("MYCOURSE-");
                for (int j = 1; j <= 10; j++) {
                    int ascii = 48 + (int) (Math.random() * 9);
                    char c = (char) ascii;
                    sb1.append(c);
                }




                DynamicObject[] Courses = BusinessDataServiceHelper.load("abq2_stu_course",
                        "id," + //课程内码
                                "abq2_mulcombofield," + //课节
                                "abq2_combofield1," + //星期
                                "abq2_integerfield," + //课程人数
                                "abq2_textfield1," + //教师
                                "abq2_classroom," + //教室
                                "abq2_combofield," + //年级
                                "abq2_integerfield1"  //已选人数
                        , new QFilter[]{new QFilter("id", QCP.is_notnull, null)});

                DynamicObject[] myCourses = BusinessDataServiceHelper.load("abq2_my_course",
                        "abq2_my_course_stu," + //课程内码
                                "name," + //课程名称
                                "abq2_textfield3," + //星期
                                "abq2_textfield1," + //教室
                                "abq2_textfield," + //教师
                                "abq2_textfield4," + //年级
                                "creator," + //学生
                                "abq2_textfield2",  //课节
                        new QFilter[]{new QFilter("creator", QCP.equals, RequestContext.get().getCurrUserId())});

                //判断现有的课程是否满员
                for (DynamicObject optionCourse : Courses) {
                    int sum = optionCourse.getInt("abq2_integerfield");
                    int already = optionCourse.getInt("abq2_integerfield1");
                    int enableNum = sum - already; //剩余人数
                    if (enableNum < 1) {
                        //如果满员，直接进行下一个课程的判断
                        continue;
                    }

                    if (myCourses != null) {
                        //failChooseCourse用于记录选课的成功与否，false代表是选课没有失败，true代表是选课失败
                        boolean failChooseCourse = false;
                        String day = optionCourse.getString("abq2_combofield1"); //目标的课程的周几
                        String classTimes = optionCourse.getString("abq2_mulcombofield"); //课节分开来
                        for (DynamicObject myCours : myCourses) {
                            //如果课程的周几和目标课程的周几冲突，还要判断课节是否冲突
                            String myday = myCours.getString("abq2_textfield3");
                            if (myday.equals(day)) {
                                //如果课程的课节与目标课程冲突
                                //判断课节是否冲突
                                String myClassTimes = myCours.getString("abq2_textfield2");
                                if (myClassTimes.equals(classTimes)) {
                                    failChooseCourse = true; //确认是无法选课
                                }
                            }
                        }
                            if (failChooseCourse) {
                                continue;
                            }
                            myCourse.set("abq2_my_course_stu", optionCourse.getPkValue());
                            myCourse.set("number", sb1);
                            myCourse.set("name", optionCourse.getString("name"));
                            myCourse.set("status", "C");
                            myCourse.set("abq2_datefield", new Date());
                            myCourse.set("creator", RequestContext.get().getCurrUserId());
                            myCourse.set("abq2_textfield", optionCourse.getString("abq2_textfield1"));
                            myCourse.set("abq2_textfield1", optionCourse.getString("abq2_classroom"));
                            myCourse.set("abq2_textfield4", optionCourse.getString("abq2_combofield"));
                            myCourse.set("abq2_textfield2", optionCourse.getString("abq2_mulcombofield"));
                            myCourse.set("abq2_textfield3", optionCourse.getString("abq2_combofield1"));

                            OperationResult saveOperate = SaveServiceHelper.saveOperate("abq2_my_course", new DynamicObject[]{myCourse}, null);
                            optionCourse.set("abq2_integerfield1", optionCourse.getInt("abq2_integerfield1") + 1);
                            SaveServiceHelper.update(optionCourse);

                            jsonObject.put("courseName", optionCourse.getString("name"));
                            jsonObject.put("teacher", optionCourse.getString("abq2_textfield1"));
                            jsonObject.put("grade", optionCourse.getString("abq2_combofield"));
                            jsonObject.put("week", optionCourse.getString("abq2_combofield1"));

                            String classTimesStr = optionCourse.getString("abq2_mulcombofield").replace(",", "  ");
                            String stuClassTimes = "第" + classTimesStr + "节课";

                            jsonObject.put("classTime", stuClassTimes);

                            jsonObject.put("classroom", optionCourse.getString("abq2_classroom"));
                            jsonArray.add(jsonObject);
                            break;
                        }

                    }
                }

                result = new HashMap();
                result.put("result", jsonArray.toJSONString());
            }
        return result;
    }
}

